﻿using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RsCode.AspNetCore;
using RsCode.AspNetCore.Jwt;
using Rswl.Common.Application;
using Rswl.Common.Application.User.Dto;
using Rswl.Common.Core;
using Rswl.Common.Core.User.Aggregate.ValueObject;

namespace Plugin.User.Controllers
{
     
    [Route("api/[controller]")]
    [ApiController]
    public class PasswordController : ControllerBase
    {

        IUserAppService userService;
        IUserDomainService userDomainService;
        public PasswordController(IUserAppService _userService, IUserDomainService userDomainService)
        {
            userService = _userService;
            this.userDomainService = userDomainService;
        }

        //密码注册
        [HttpPost("register")]
        public async Task<object> PasswordRegisterAsync(PasswordRegiserDto dto)
        {
            return await userService.PasswordRegisterAsync(dto);
        }

        //密码登录
        [HttpPost("login")]
        public async Task<AccessTokenInfo> UserLoginAsync(PasswordLoginDto dto)
        {
            return await userService.PasswordLoginAsync(dto);
        }

        //忘记密码
        [HttpPost("forget")]
        public async Task<object>ForgetPasswordAsync(PasswordForgetDto dto)
        {
            await userDomainService.ForgetPasswordAsync(dto.Adapt<PasswordForgetValueObject>());
            return "Ok";
        }
        //登录密码找回
        [HttpPost("find")]
        public async Task<object> FindPasswordAsync(PasswordFindDto dto)
        {
            return await userService.FindPasswordAsync(dto);
        }

        //更换密码
        [Idempotent]
        [Authorize]
        [HttpPost("change")]
        public async Task<string> ChangePasswordAsync(PasswordChangeDto dto)
        {
            await userService.ChangeLoginPasswordAsync(dto);
            return "ok";
        }
    }
}
